package com.example.venue_reservation_service.utils;

import jakarta.annotation.Resource;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

@Component
public class ScheduleUtil {

    @Resource
    private TaskScheduler taskScheduler;

    public ScheduleUtil(TaskScheduler taskScheduler) {
        this.taskScheduler = taskScheduler;
    }

    public void scheduleOneTimeTask(Runnable task, LocalDate executionDate, LocalTime executionTime) {
        LocalDateTime executionDateTime = LocalDateTime.of(executionDate, executionTime);
        Date executionDateObj = Date.from(executionDateTime.atZone(ZoneId.systemDefault()).toInstant());

        if (executionDateObj.before(new Date())) {
            System.out.println("指定的执行时间已过，任务不会被安排执行。");
            return;
        }
        taskScheduler.schedule(task, executionDateObj);
    }
}
